import { _decorator, AnimationState, CapsuleCollider, Component, ICollisionEvent, instantiate, ITriggerEvent, LightProbeInfo, log, Material, MeshRenderer, Node, Prefab, resources, RigidBody, SkeletalAnimation, SkinnedMeshRenderer, tween, v3, Vec3 } from 'cc';
import { GameManager, Group } from './Manager/GameManager';
import { PlayerController } from './Manager/PlayerController';
import { Player } from './Player';
import { Card } from './Card';
import { AudioManager } from './Manager/AudioManager';
import Banner from './Manager/Banner';
const { ccclass, property } = _decorator;

@ccclass('Monster')
export class Monster extends Component {

    MonsterRigibody: RigidBody;
    MonsterAni: SkeletalAnimation;
    FollowOffset = v3();
    Following: boolean = false;
    PowerValue: number = 0;
    IsEnmey: boolean = false;//默认为我方怪物
    FightPoint = v3();
    Name: string = "";

    LevelStage: number = 1;//等级阶段

    onEnable() {

    }

    onLoad() {
        this.MonsterRigibody = this.node.getComponent(RigidBody);
        this.MonsterAni = this.node.getComponentInChildren(SkeletalAnimation);
        this.MonsterAni.on(SkeletalAnimation.EventType.FINISHED, this.AniFinished, this);

        let collider = this.node.getComponent(CapsuleCollider);
        collider.on("onTriggerEnter", this.onTriggerEnter, this);
    }

    AniFinished(type, state: AnimationState) {
        switch (state.clip.name) {
            case "Jump":
                {
                    if (GameManager.Instance.IsEnterFight) {
                        this.MonsterAni.crossFade("Idle");
                    }
                    else {
                        this.Following = true;
                        this.MonsterAni.crossFade("Run");
                    }
                }
                break;
            case "Attack":
                {
                    this.MonsterAni.crossFade("Idle");
                }
                break;
        }
    }
    start() {

    }

    IsJoin: boolean = false;
    update(deltaTime: number) {
        if (this.Following) {
            let playerPos = GameManager.Instance.PlayerInit.children[0].getWorldPosition();
            let targetPoint = v3(playerPos.x + this.FollowOffset.x, 0, playerPos.z + this.FollowOffset.z);
            let monsterPos = this.node.getWorldPosition();
            let duration: number = 0;
            if (this.IsJoin) {
                duration = 0.8;
            }
            else {
                if (Vec3.distance(targetPoint, monsterPos) <= 0.1) {
                    this.IsJoin = true;
                    duration = 0.8;
                }
                else {
                    duration = 1 / (Vec3.distance(targetPoint, monsterPos) * 8);
                    if (duration >= 0.8) {
                        duration = 0.8;
                    }
                }
            }
            Vec3.lerp(monsterPos, monsterPos, targetPoint, duration);
            this.node.setWorldPosition(monsterPos);
        }
    }


    NeedHeCheng: boolean = false;//当前怪物是否可以合成
    onTriggerEnter(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == Group.Monster && this.NeedHeCheng && this.Following && this.node.name == event.otherCollider.node.name) {
            this.Following = false;
            Banner.Instance.ShowNativeAd();
            let otherNd = event.otherCollider.node;
            switch (otherNd.getComponent(Monster).LevelStage) {
                case 1:
                    {
                        AudioManager.Instance.playSound("Audio/Init");
                        resources.load("Prefabs/Monster/" + this.node.name, Prefab, (err, pre) => {
                            let monster = instantiate(pre);
                            monster.setParent(GameManager.Instance.MonsterInit);
                            monster.getComponent(Monster).MonsterAni.crossFade("Jump");
                            resources.load("Materials/Golden", Material, (err, mat) => {
                                monster.getChildByName("Model").getComponentInChildren(SkinnedMeshRenderer).setMaterialInstance(mat, 0);
                            })
                            let worldPos = otherNd.getWorldPosition();
                            monster.setWorldPosition(worldPos);
                            monster.getComponent(Monster).FollowOffset = otherNd.getComponent(Monster).FollowOffset;
                            monster.getComponent(Monster).Name = "黄金" + otherNd.name;
                            monster.getComponent(Monster).LevelStage++;

                            let haveMonsters = GameManager.Instance.PlayerInit.children[0].getComponent(Player).HaveMonsters;
                            monster.getComponent(Monster).PowerValue = GameManager.Instance.MonsterData.json[monster.getComponent(Monster).Name];
                            let index = haveMonsters.findIndex(data => data == this.node);
                            if (index !== -1) {
                                haveMonsters.splice(index, 1);
                            }
                            index = haveMonsters.findIndex(data => data == otherNd);
                            if (index !== -1) {
                                haveMonsters.splice(index, 1);
                            }
                            haveMonsters.push(monster);
                            let cardInit = GameManager.Instance.Level.getChildByName("CardInit") ? GameManager.Instance.Level.getChildByName("CardInit") : GameManager.Instance.RoadInit.getChildByName("CardInit");
                            for (let i = 0; i < cardInit.children.length; i++) {
                                if (otherNd.name == cardInit.children[i].getComponentInChildren(Card).Name) {
                                    cardInit.children[i].getChildByName("Card").getComponentInChildren(MeshRenderer).getMaterialInstance(0).setProperty("rimLightStrengthen", 10);
                                    cardInit.children[i].getChildByName("Card").getChildByName("Card").getChildByName("Star2").active = false;
                                    cardInit.children[i].getChildByName("Card").getChildByName("Card").getChildByName("Star3").active = true;
                                }
                            }
                            otherNd.destroy();
                            this.node.destroy();
                        })
                    }
                    break;
                case 2:
                    {
                        AudioManager.Instance.playSound("Audio/Init");
                        resources.load("Prefabs/Monster/" + this.node.name, Prefab, (err, pre) => {
                            let monster = instantiate(pre);
                            monster.setParent(GameManager.Instance.MonsterInit);
                            monster.getComponent(Monster).MonsterAni.crossFade("Jump");
                            resources.load("Materials/Diamond", Material, (err, mat) => {
                                monster.getChildByName("Model").getComponentInChildren(SkinnedMeshRenderer).setMaterialInstance(mat, 0);
                            })
                            let worldPos = event.otherCollider.node.getWorldPosition();
                            monster.setWorldPosition(worldPos);
                            monster.getComponent(Monster).FollowOffset = event.otherCollider.node.getComponent(Monster).FollowOffset;
                            monster.getComponent(Monster).Name = "钻石" + event.otherCollider.node.name;
                            monster.getComponent(Monster).LevelStage++;

                            let haveMonsters = GameManager.Instance.PlayerInit.children[0].getComponent(Player).HaveMonsters;
                            monster.getComponent(Monster).PowerValue = GameManager.Instance.MonsterData.json[monster.getComponent(Monster).Name];
                            let index = haveMonsters.findIndex(data => data == this.node);
                            if (index !== -1) {
                                haveMonsters.splice(index, 1);
                            }
                            index = haveMonsters.findIndex(data => data == event.otherCollider.node);
                            if (index !== -1) {
                                haveMonsters.splice(index, 1);
                            }
                            haveMonsters.push(monster);
                            event.otherCollider.node.active = false;
                            this.node.active = false;
                        })
                    }
                    break;
            }
        }
    }
}


